package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 新增地址
     *
     * @param addressBook
     */
    @Override
    public void save(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookMapper.insert(addressBook);
    }

    /**
     * 查询所有地址
     *
     * @return
     */
    @Override
    public List<AddressBook> list() {
        Long userId = BaseContext.getCurrentId();
        return addressBookMapper.list(userId);

    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @Override
    public AddressBook getAddressBook(AddressBook addressBook) {
        AddressBook defaultAddressbook = new AddressBook();
        defaultAddressbook.setUserId(BaseContext.getCurrentId());
        defaultAddressbook.setIsDefault(StatusConstant.ENABLE);
        return addressBookMapper.getDefaultAddressBook(defaultAddressbook);
    }

    /**
     * 修改默认地址
     *
     * @param addressBook
     */
    @Override
    public void setAddressBook(AddressBook addressBook) {
        addressBook.setIsDefault(StatusConstant.DISABLE);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateDafaultByUserId(addressBook);

        addressBook.setIsDefault(StatusConstant.ENABLE);
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @Override
    public AddressBook selectById(Long id) {
        Long userId = BaseContext.getCurrentId();
        return addressBookMapper.selectById(id, userId);
    }

    /**
     * 修改地址簿
     *
     * @param addressBook
     */
    @Override
    public void update(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据id删除地址
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        Long userId = BaseContext.getCurrentId();
        addressBookMapper.deleteById(id, userId);
    }


}
